package com.anche.cywebnew.Controller.configManager;

import com.anche.cywebnew.Pojo.configManager.CySysCode;
import com.anche.cywebnew.Pojo.configManager.SysOptions;
import com.anche.cywebnew.Service.configManager.SysCodeService;
import com.anche.cywebnew.Service.configManager.SysOptionService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/config/syscode")
public class SysCodeController {

    @Autowired
    private SysCodeService sysCodeService;

    private static final Logger logger = LoggerFactory.getLogger(SysCodeController.class);


    @RequestMapping("/manager")
    public String toSysCodePage() {
        return "configManager/syscode";
    }

    @GetMapping("/sysCodePager")
    @ResponseBody
    public Object getSysCodePager(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, CySysCode cySysCode) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<CySysCode> sysCodePager = sysCodeService.getSysCodePager(page, limit, cySysCode);
            logger.info("<查询数据字典列表成功>");
            map.put("code", "0");
            map.put("count", sysCodePager.getTotal());
            map.put("msg", "查询成功");
            map.put("data", sysCodePager.getResult());
        } catch (Exception e) {
            logger.error("<查询数据字典列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            map.put("code", "1");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @GetMapping("/list")
    @ResponseBody
    public Object getSysCodeList(CySysCode cySysCode) {
        try {
            List<CySysCode> sysCodes = sysCodeService.getSysCodeList(cySysCode);
            logger.info("<查询数据字典列表成功>");
            return ResultUtil.success(sysCodes);
        } catch (Exception e) {
            logger.error("<查询数据字典列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/sysCodeInfo", method = RequestMethod.POST)
    @ResponseBody
    @OptLog(title = "添加数据字典信息", type = OptType.ADD)
    public Object addSysCode(CySysCode cySysCode) {
        try {
            sysCodeService.addSysCode(cySysCode);
            logger.info("<新增[{}]数据字典成功>", cySysCode.getOi_name());
            return ResultUtil.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<新增[{}]数据字典异常>:{}", cySysCode.getOi_name(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/sysCodeInfo", method = RequestMethod.PUT)
    @ResponseBody
    @OptLog(title = "更新数据字典信息", type = OptType.UPDATE)
    public Object setSysCode(CySysCode cySysCode) {
        try {
            sysCodeService.setSysCode(cySysCode);
            logger.info("<更新[{}]数据字典成功>", cySysCode.getOi_name());
            return ResultUtil.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新[{}]数据字典异常>:{}", cySysCode.getOi_name(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }


    @RequestMapping(value = "/sysCodeInfo", method = RequestMethod.DELETE)
    @ResponseBody
    @OptLog(title = "删除数据字典信息", type = OptType.DEL)
    public Object delSysCode(@RequestParam("id") String id) {
        try {
            sysCodeService.delSysCode(id);
            return ResultUtil.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<删除id为 " + id + " 的数据字典异常>:{}", e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping("/oiNameList")
    @ResponseBody
    public Object getOiNameList() {
        try {
            List<String> syscodes = sysCodeService.getOiNameList();
            return ResultUtil.success(syscodes);
        } catch (Exception e) {
            return ResultUtil.error(0, e.getMessage());
        }
    }

}
